import 'package:yt_devops/model/device_model.dart';
import 'package:yt_devops/model/install_provider_model.dart';
import 'package:yt_devops/model/user_model.dart';
import 'package:yt_devops/pages/devices/device_list/device_list_model.dart';
import '../base/jsons_convert_content.dart';
import 'paging_datas.dart';

PagingDatas<T> $PagingDatasFromJson<T>(Map<String, dynamic> json) {
  final PagingDatas<T> pagingData = PagingDatas<T>();
  final int? current = jsonsConvert.convert<int>(json['current']);
  if (current != null) {
    pagingData.pageNum = current;
  }
  final bool? hitCount = jsonsConvert.convert<bool>(json['hitCount']);
  if (hitCount != null) {
    pagingData.hitCount = hitCount;
  }
  final int? pages = jsonsConvert.convert<int>(json['pages']);
  if (pages != null) {
    pagingData.pages = pages;
  }
  final List<T>? records = jsonsConvert.convertListNotNull<T>(json['list']);
  if (records != null) {
    pagingData.list = records;
  }
  final bool? searchCount = jsonsConvert.convert<bool>(json['searchCount']);
  if (searchCount != null) {
    pagingData.searchCount = searchCount;
  }
  final int? size = jsonsConvert.convert<int>(json['size']);
  if (size != null) {
    pagingData.size = size;
  }
  final int? total = jsonsConvert.convert<int>(json['total']);
  if (total != null) {
    pagingData.total = total;
  }
  return pagingData;
}

Map<String, dynamic> $PagingDatasToJson(PagingDatas entity) {
  final Map<String, dynamic> data = <String, dynamic>{};
  data['pageNum'] = entity.pageNum;
  data['hitCount'] = entity.hitCount;
  data['pages'] = entity.pages;
  data['list'] = entity.list;
  data['searchCount'] = entity.searchCount;
  data['size'] = entity.size;
  data['total'] = entity.total;
  return data;
}

M? pagingDataFromJsonSingle<M>(Map<String, dynamic> json) {
  String type = M.toString();
  String genericType = type.substring(type.indexOf("<") + 1, type.length - 1);

  if ((UserModel).toString() == genericType) {
    PagingDatas<UserModel> pagingData = PagingDatas<UserModel>.fromJson(json);
    return pagingData as M;
  }

  if ((InstallProviderModel).toString() == genericType) {
    PagingDatas<InstallProviderModel> pagingData =
        PagingDatas<InstallProviderModel>.fromJson(json);
    return pagingData as M;
  }

  if ((DeviceModel).toString() == genericType) {
    PagingDatas<DeviceModel> pagingData =
        PagingDatas<DeviceModel>.fromJson(json);
    return pagingData as M;
  }

  if ((DeviceListModel).toString() == genericType) {
    PagingDatas<DeviceListModel> pagingData =
        PagingDatas<DeviceListModel>.fromJson(json);
    return pagingData as M;
  }

  return PagingDatas.fromJson(json) as M;
}
